<template>
  <div class="container">
    <div class="form-box">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-input v-model="form.id" type="hidden"></el-input>
        <el-input v-model="form.expression" type="hidden"></el-input>
        <el-form-item label="规则编码" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>

        <el-form-item label="规则名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="监控规则" prop="expitemchars">
          <el-input
            v-model="form.expitemchars"
            suffix-icon="el-icon-setting"
            :readonly="true"
            placeholder="请选择"
            @click.native="setPersion"
          ></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="1" model-value="status">启用</el-radio>
            <el-radio label="0" model-value="status">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报警级别" prop="level">
          <el-radio-group v-model="form.level">
            <el-radio label="1" model-value="level">预警</el-radio>
            <el-radio label="2" model-value="level">报警</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="descrpt">
          <el-input v-model="form.descrpt"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </div>
    <Express ref="express" @getexpress="getexpress"></Express>
  </div>
</template>

<script>
  import { doEdit } from '@/api/alarm'
  import Express from './Express'
  export default {
    name: 'UserManagementEdit',
    components: { Express },
    props: {
      infoData: {
        type: Object,
        default: () => {
          return {}
        },
      },
    },
    data() {
      return {
        form: {
          status: '1',
          id: '',
          name: '',
          code: '',
          expression: '',
          expitemchars: '',
          level: '1',
          descrpt: '',
        },
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
          code: [
            { required: true, trigger: 'blur', message: '请输入编码' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
          expitemchars: [
            { required: true, trigger: 'blur', message: '请输入监控规则' },
            {
              min: 5,
              max: 200,
              message: '长度在 5 到 200 个字符',
              trigger: 'blur',
            },
          ],
        },
        title: '',
      }
    },
    created() {
      this.forminitload()
    },
    methods: {
      forminitload() {
        if (this.infoData) {
          this.form = Object.assign(this.form, this.infoData)
          this.form.expitemchars = this.form.expshowflag
        }
      },
      getexpress(value) {
        this.form.expression = JSON.stringify(value.expitems)
        this.form.expitemchars = value.expchar
      },
      setPersion() {
        this.$refs['express'].showEdit()
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        //this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.$emit('change-step', 1)
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.close()
            this.$emit('change-step', 1)
          } else {
            return false
          }
        })
      },
    },
  }
</script>
